package itheima.com.Day17;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ElementDemo {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("Master//xml/students.xml"));
        Element rootElement = document.getRootElement();
        ArrayList<Student> list = new ArrayList<>();

        List<Element> studentelements = rootElement.elements("student");
        for (Element studentelement : studentelements) {
            Attribute attribute = studentelement.attribute("id");
            String id = attribute.getValue();

            Element nameElement = studentelement.element("name");
            String name = nameElement.getText();


            Element ageElement = studentelement.element("age");
            String age = ageElement.getText();

            Student s = new Student(id,name,Integer.parseInt(age));
            list.add(s);
        }

        for (Student student : list) {
            System.out.println(student);
        }


    }
}
